var  params={};
var deparmentId;
$(function () {
    initDataGrid();
   // alert(exchangeTel("18995758391"));
    $('#treeList').tree({
        url:"/department/treeList",
        onClick: function(node){
            //alert(node.id);  // 在用户点击的时候提示
            $("input[name='departmentId']").val(node.id)
            deparmentId=node.id;
            query();
            $('#table').datagrid('reload'); //设置好查询参数 reload
        }
    });

})


function query(){
    debugger;
    var params = $('#table').datagrid('options').queryParams; //先取得 datagrid 的查询参数
    var fields =$('#searchFrom').serializeArray(); //自动序列化表单元素为JSON对象
    $.each( fields, function(i, field){
        params[field.name] = field.value; //设置查询参数
    });
    $('#table').datagrid('reload'); //设置好查询参数 reload
}

function reset() {
    $("#searchFrom").form('clear');
    $("input[name='departmentId']").val(deparmentId)
    query();
}


function  initDataGrid() {
    $("#table").datagrid({
        toolbar:"#tb",
        pagination:true,
        rownumbers:true,
        checkOnSelect:true,
        url:"/user/showUserList",
        striped:true,
        frozenColumns:[[{
            field:"id",
            checkbox:true
        }]],
        columns:[[
            {field:'username',title:'用户名',width:'10%',align:'center'},
            {field:'sex',title:'性别',width:'5%',align:'center',
                formatter:function (value,row,index) {
                    if(value==1){
                        return "男"
                    }else if(value==2){
                        return "女"
                    }
                }
            
            },
            {field:'departmentName',title:'部门名称',width:'10%',align:'center'},
            {field:'job',title:'职位',width:'10%',align:'center',
                formatter:function (value,row,index) {
                    if(value==2){
                        return "部门经理";
                    }else if(value==1){
                        return "员工";
                    }
                }

            },
            {field:'telephone',title:'电话号码',width:'10%',align:'center',
                formatter:function (value,row,index) {
                    return  exchangeTel(""+value+"");
                }
            },
            {field:'email',title:'邮箱',width:'20%',align:'center'},
            {field:'address',title:'地址',width:'20%',align:'center'},
          /*  {field:'lastIp',title:'最后一次登录ip地址',width:'20%',align:'center'},*/
            {field:'createTime',title:'创建时间',width:'20%',align:'center',formatter:formatDate}
        ]]
    })
}
/**
 * 隐藏部分手机号码
 * @param value
 * @returns {string | void}
 */
function  exchangeTel(value) {
    return value.replace(/(\d{3})(\d{4})(\d{4})/,"$1****$3");
}



/**
 * 删除用户
 */
function deleteUser() {
    deleteData("table","用户","user/delete");
}

